﻿// ==========================================================================
// Main.cs
// Silverlight Shapes Library
// ==========================================================================
// Copyright (c) Silverlight Shapes Development Group
// All rights reserved.
// ==========================================================================
using System;
using System.IO;
using System.Linq;
using System.Windows.Markup;
using SD.Samples.Configuration;

namespace CopySource
{
    class Program
    {
        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);

            Console.WriteLine("Parameters: {0} {1}", args.Length, args);

            string workingDirectory = args[0];
            string configFile = args[1];
            string sourceFolders = args[2];

            SampleRepository sampleRepository = null;

            using (FileStream fileStream = new FileStream(Path.Combine(workingDirectory, configFile), FileMode.Open, FileAccess.Read))
            {
                sampleRepository = (SampleRepository)XamlReader.Load(fileStream);
            }

            foreach (Sample sample in sampleRepository.SampleCatagories.SelectMany(x => x.Samples))
            {
                if (!string.IsNullOrEmpty(sample.ProjectFile))
                {
                    string sampleFolder = Path.GetFullPath(Path.Combine(workingDirectory, sourceFolders, sample.ProjectFile));

                    if (!Directory.Exists(sampleFolder))
                    {
                        Directory.CreateDirectory(sampleFolder);
                    }

                    foreach (SourceFile sourceFile in sample.SourceFiles)
                    {
                        try
                        {
                            string sourceFileName = sourceFile.FileName.Replace("\\", "_");

                            string source = Path.GetFullPath(Path.Combine(workingDirectory, sample.ProjectFile, sourceFile.FileName));
                            string target = Path.GetFullPath(Path.Combine(sampleFolder, sourceFileName + ".txt"));

                            if (File.Exists(target))
                            {
                                File.SetAttributes(target, FileAttributes.Normal);
                            }

                            if (File.Exists(source))
                            {
                                File.Copy(source, target, true);
                            }
                        }
                        catch (ArgumentException)
                        {
                        }
                    }
                }
            }
        }

        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Console.WriteLine(e.ExceptionObject);
        }
    }
}
